package com.cee.admin.modules.pm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cee.common.web.result.Result;
import com.cee.domain.modules.pm.entity.PmNews;
import com.cee.domain.modules.pm.vo.PmNewsVo;
import com.cee.domain.modules.sys.entity.User;
import com.cee.ds.modules.pm.service.IPmNewsService;
import com.cee.ds.modules.sys.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.cee.admin.common.BaseController;

import java.util.Date;


/**
 * <p>
 * 新闻表 前端控制器
 * </p>
 *
 * @author autoGenerator
 * @since 2024-03-08
 */
@Tag(name = "门户管理-新闻发布管理")
@RestController
@RequestMapping("/api/pm/news")
public class PmNewsController extends BaseController {

    @Autowired
    private IPmNewsService pmNewsService;

    @Autowired
    private IUserService iUserService;

    @Operation(summary = "分页查询新闻")
    @GetMapping("/page")
    public Result<Page<PmNewsVo>> page(@RequestParam Long current, @RequestParam Long size, PmNewsVo query){
        return success(pmNewsService.queryPageVo(new Page<>(current, size), query));
    }

    @Operation(summary = "新增新闻")
    @PostMapping("/save")
    @RequiresPermissions("pm:news:save")
    public Result<Boolean> save(@RequestBody PmNews news){
        news.setStatus("0");
        return success(pmNewsService.saveOrUpdateNews(news));
    }

    @Operation(summary = "修改新闻")
    @PostMapping("/update")
    @RequiresPermissions("pm:news:update")
    public Result<Boolean> update(@RequestBody PmNews news){
        return success(pmNewsService.saveOrUpdateNews(news));
    }

    @Operation(summary = "发布新闻")
    @PostMapping("/publish/{id}")
    @RequiresPermissions("pm:news:publish")
    public Result<Boolean> publish(@PathVariable("id") Long id){
        PmNews news = new PmNews();
        news.setId(id);
        news.setStatus("1");
        news.setPublishTime(new Date());
        return success(pmNewsService.updateById(news));
    }

    @Operation(summary = "查看新闻详情")
    @GetMapping("/get/{id}")
    @RequiresPermissions("pm:news:get")
    public Result<PmNewsVo> get(@PathVariable("id") Long id){
        PmNews news = pmNewsService.getById(id);
        if (news == null){
            return error("该新闻不存在");
        }
        User user = iUserService.getById(news.getCreateUserId());
        PmNewsVo newsVo = new PmNewsVo();
        BeanUtils.copyProperties(news, newsVo);
        newsVo.setCreateUserName(user.getName());
        return success(newsVo);
    }

    @Operation(summary = "删除新闻")
    @DeleteMapping("/delete/{id}")
    @RequiresPermissions("pm:news:delete")
    public Result<Boolean> delete(@PathVariable("id") Long id){
        return success(pmNewsService.removeNews(id));
    }


}
